<?php
//=======================================
//###################################
// Created by _DmG_
// Feel free to edit this, or take out my
// name. I don't care at all ... just looking
// to help out the community.
//###################################
//=======================================

if (!defined("INSWIFT")) {
trigger_error("Unable to process $PHP_SELF", E_USER_ERROR);
}

/**
* Initialization function. You can connect to your database etc over here.
*/
function loginShareInit()
{
global $loginshare;

$loginshare->moduleloaded = true;
}

/**
* Authorize a user based on email and password
*/
function loginShareAuthorize($username, $password)
{
	global $dbCore, $_SWIFT, $loginshare, $settings;
	
	$_loginshare = $settings->getSection("loginshare");
	$dbLoginShare = new dbCore($_loginshare["fsr_hostname"], $_loginshare["fsr_user"], $_loginshare["fsr_pass"], $_loginshare["fsr_db"], "mysql", false);
	
	$_user = $dbLoginShare->queryFetch("SELECT * FROM `". $_loginshare["fsr_prefix"] . "users` WHERE `username` = '". $dbCore->escape($username) ."';");
	if (empty($_user["id"]))
	{
		return false;
	}
	
	$userpassword = md5($password);
	
	$regpassword = substr(buildHash(),0,8);
	
	if ($_user["password"] == $userpassword && !empty($_user["password"]))
	{
		// We have a match, Seems like a valid user.. now see if he is registered..
		$userid = getLoginShareUser(LOGINAPI_FSR, $_user["id"]);
		if (!$userid)
		{
			// Not registered, Register him
			$userid = insertUser(true, $_user["email"], $regpassword, $_SWIFT["tgroup"]["regusergroupid"], LOGINAPI_FSR, $_user["id"], $_user["username"], $_SWIFT["tgroup"]["languageid"], 0, false, 1, true);
		}
		
		if (!$userid)
		{
			return false;
		}
		
		$_swiftuser = $loginshare->loadSWIFTUser($userid);
		if (!$_swiftuser)
		{
			return false;
		}
		
		$_SWIFT["user"] = $_swiftuser;
		
		return $_swiftuser["userid"];
	}
	
	return false;
}

/**
* Return the Unique User ID of the current user
*/
function loginShareUserID()
{
global $_SWIFT;

if (empty($_SWIFT["user"]["userid"]))
{
return false;
} else {
return $_SWIFT["user"]["userid"];
}
}

/**
* Logout the current user
*/
function loginShareLogout()
{
global $session, $_SWIFT;

$session->updateSession($_SWIFT["session"]["sessionid"], 0);

return true;
}

/**
* Load the user credentials into current workspace. The following variables should be declared for proper working:
* userid - User id that is set in the "users" table
* fullname
* email - Array
* password (MD5 Hashed)
* usergroupid - If this is not set, then it will use the default registered user group for this template group
*/
function loginShareLoadUser()
{
global $dbCore, $_SWIFT, $loginshare;

if (empty($_SWIFT["session"]["typeid"]))
{
$_SWIFT["user"]["loggedin"] = false;
return false;
}

$_user = $loginshare->loadSWIFTUser($_SWIFT["session"]["typeid"]);
if (!$_user)
{
$_SWIFT["user"]["loggedin"] = false;

return false;
}

$_SWIFT["user"] = $_user;

return true;
}

/**
* Renders the Login Share Form
*/
function renderLoginShareForm()
{
global $_SWIFT;

$forms = array();

$forms[0]["title"] = $_SWIFT["language"]["hostname"];
$forms[0]["name"] = "fsr_hostname";
$forms[1]["title"] = $_SWIFT["language"]["dbname"];
$forms[1]["name"] = "fsr_db";
$forms[2]["title"] = $_SWIFT["language"]["dbuser"];
$forms[2]["name"] = "fsr_user";
$forms[3]["title"] = $_SWIFT["language"]["dbpass"];
$forms[3]["name"] = "fsr_pass";
$forms[4]["title"] = "Table Prefix (Include underscore! \"_\")";
$forms[4]["name"] = "fsr_prefix";

return $forms;

}
?>